// 
//  prefssitesui.js
//  firefox
//  
//  Created by Zak on 2008-07-03.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntPrefsSitesUi = {
    
	/**
	 * On options_siteslist.xul Load
	 * @param param1   Description
	 */
	init: function (doc)
    {
        this.doc = doc;
        this.generateList();
        if (AntLib.getOsName() == "Darwin")
            $('antBarPrefsSitesListOkButton').setAttribute('hidden', false);
    },
    
    /**
     * Add an richlistitem to the richlistbox
     */
    appendItem: function (url, id)
    {
        var listBox = $("antSitesList", this.doc);
        var listItem = this.doc.createElement('richlistitem');
        var listLabel = this.doc.createElement('label');
        var listButton = this.doc.createElement('button');

      	listItem.setAttribute('id', 'antSitesListItem-'+id);

      	listLabel.setAttribute('id', 'antSitesListLabel-'+id);
      	listLabel.setAttribute('value', url);
      	listLabel.setAttribute('flex', 1);

      	listButton.setAttribute('id', 'antSitesListButton-'+id);
        listButton.setAttribute('class', 'antSitesDeleteButton');
        listButton.setAttribute('oncommand', 'AntPrefsSitesUi.removeSite("' + url + '");');

      	listItem.appendChild(listLabel);
      	listItem.appendChild(listButton);

      	listBox.appendChild(listItem);
    },
    
    /**
     * Wipe out the list
     */
    clearList: function ()
    {
        var listBox = $("antSitesList", this.doc);
        
				while (listBox.firstChild)
					listBox.removeChild(listBox.firstChild);
    },

    /**
     * Create the list based on the current preferances
     */
    generateList: function ()
    {
        var sitesList = AntPrefs.getSitesList();
        
        if (sitesList)
        {
            for (var i = 0; i < sitesList.length; i++)
            {
                this.appendItem(sitesList[i], i);
            }
        }
    },

    /**
     * Cleanup & generate the list
     */
    redrawList: function ()
    {
        this.clearList();
        this.generateList();
    },

	/**
	 * Remove a site from the list & prefs
	 */
	removeSite: function (domain)
	{
	 	AntPrefs.removeDomain(domain);
		this.redrawList();
	}
}
